package judesart.app.client.profile.ui;

import judesart.app.client.profile.CountryModel;
import judesart.app.client.profile.ProfileController;
import judesart.app.client.profile.ProfileControllerAsync;
import judesart.app.client.widget.TopInfo;
import judesart.app.domain.Address;
import judesart.app.domain.Artist;

import com.allen_sauer.gwt.log.client.Log;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.InfoConfig;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ContactsPanel extends LayoutContainer
{
    private final class SaveArtistCallback implements AsyncCallback<Artist>
    {
        @Override
        public void onSuccess(Artist result)
        {
            TopInfo.display("Save Ok", "Profile information saved.");
        }

        @Override
        public void onFailure(Throwable caught)
        {
            InfoConfig ic = new InfoConfig("Save Error",
                    "Unable to save profile information...");
            ic.display = 5000;
            TopInfo.display(ic);
        }
    }

    ProfileControllerAsync profileController = GWT.create(ProfileController.class);
    Artist artist;
    boolean isNew;
    private FormPanel form;
    private Button btnSave;
    private TextField<String> address1Field;
    private TextField<String> address2Field;
    private TextField<String> cityField;
    private ComboBox<CountryModel> countryCombo;
    private TextField<String> mobileField;
    private TextField<String> phoneField;
    private TextField<String> facebookUserField;
    private TextField<String> flickrUserField;
    private TextField<String> zipCodeField;
    private TextField<String> regionField;

    public ContactsPanel()
    {
        setStyleAttribute("margin", "0");
        add(createForm());
        refresh();
    }

    private void refresh()
    {
        profileController.getArtist(new AsyncCallback<Artist>()
        {

            @Override
            public void onSuccess(Artist result)
            {
                if (result == null)
                {
                    Log.debug(" artist null creating new");
                    result = new Artist();
                    Address address = new Address();
                    result.setAddress(address);
                    setIsNew(true);
                }
                setArtist(result);
            }

            @Override
            public void onFailure(Throwable caught)
            {
                TopInfo.display("Error", "Unable to get Artist information");
            }
        });
    }

    private void save()
    {
        bindToModel();
        if (isNew)
        {
            profileController.createArtist(artist, new SaveArtistCallback());
        } else
        {
            profileController.updateArtist(artist, new SaveArtistCallback());
        }
    }

    private FormPanel createForm()
    {
        form = new FormPanel();
        form.setFrame(true);
        form.setHeading("Contacts");
        form.setWidth(800);
        form.setLayout(new FlowLayout());

        FieldSet addressFieldSet = new FieldSet();
        addressFieldSet.setHeading("Address");
        addressFieldSet.setCollapsible(true);

        FormLayout layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        layout.setLabelSeparator("");
        addressFieldSet.setLayout(layout);

        address1Field = new TextField<String>();
        address1Field.setFieldLabel("Street Address 1");
        addressFieldSet.add(address1Field, new FormData("100%"));
        address1Field.setName("address.address1");

        address2Field = new TextField<String>();
        address2Field.setFieldLabel("Street Address 2");
        addressFieldSet.add(address2Field, new FormData("100%"));

        LayoutContainer main = new LayoutContainer();
        main.setLayout(new ColumnLayout());

        FormLayout layoutl = new FormLayout();
        layoutl.setLabelAlign(LabelAlign.TOP);
        layoutl.setLabelSeparator("");
        LayoutContainer left = new LayoutContainer();
        left.setStyleAttribute("paddingRight", "10px");
        left.setLayout(layoutl);

        LayoutContainer right = new LayoutContainer();
        right.setStyleAttribute("paddingLeft", "10px");
        FormLayout layoutr = new FormLayout();
        layoutr.setLabelAlign(LabelAlign.TOP);
        layoutr.setLabelSeparator("");
        right.setLayout(layoutr);

        cityField = new TextField<String>();
        cityField.setFieldLabel("City");
        left.add(cityField, new FormData("100%"));
        cityField.setName("address.city");

        regionField = new TextField<String>();
        regionField.setFieldLabel("Region");
        right.add(regionField, new FormData("100%"));

        zipCodeField = new TextField<String>();
        zipCodeField.setFieldLabel("Zip Code");
        left.add(zipCodeField, new FormData("100%"));

        ListStore<CountryModel> store = new ListStore<CountryModel>();
        store.add(CountryModel.getCountries());

        countryCombo = new ComboBox<CountryModel>();
        countryCombo.setFieldLabel("Country");
        countryCombo.setDisplayField("title");
        countryCombo.setTriggerAction(TriggerAction.ALL);
        countryCombo.setStore(store);
        countryCombo.setForceSelection(true);
        right.add(countryCombo, new FormData("100%"));

        main.add(left, new ColumnData(.5));
        main.add(right, new ColumnData(.5));
        addressFieldSet.add(main, new FormData("100%"));

        form.add(addressFieldSet);

        FieldSet fieldSet = new FieldSet();
        fieldSet.setHeading("Other");
        fieldSet.setCollapsible(true);

        FormLayout layout1 = new FormLayout();
        layout1.setLabelAlign(LabelAlign.TOP);
        layout1.setLabelSeparator("");
        fieldSet.setLayout(layout1);

        main = new LayoutContainer();
        main.setLayout(new ColumnLayout());

        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        layout.setLabelSeparator("");
        left = new LayoutContainer();
        left.setStyleAttribute("paddingRight", "10px");
        left.setLayout(layout);

        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        layout.setLabelSeparator("");
        right = new LayoutContainer();
        right.setStyleAttribute("paddingLeft", "10px");
        right.setLayout(layout);

        phoneField = new TextField<String>();
        phoneField.setFieldLabel("Phone");
        left.add(phoneField, new FormData("100%"));
        phoneField.setName("phone");

        mobileField = new TextField<String>();
        mobileField.setFieldLabel("Mobile");
        right.add(mobileField, new FormData("100%"));

        flickrUserField = new TextField<String>();
        flickrUserField.setFieldLabel("Flickr User");
        left.add(flickrUserField, new FormData("100%"));

        facebookUserField = new TextField<String>();
        facebookUserField.setFieldLabel("Facebook User");
        right.add(facebookUserField, new FormData("100%"));

        main.add(left, new ColumnData(.5));
        main.add(right, new ColumnData(.5));
        fieldSet.add(main, new FormData("100%"));

        form.add(fieldSet);

        form.setButtonAlign(HorizontalAlignment.CENTER);
        btnSave = new Button("Save");
        btnSave.addListener(Events.Select, new Listener<ButtonEvent>()
        {
            public void handleEvent(ButtonEvent e)
            {
                save();
            }
        });
        form.addButton(btnSave);
        Button cancelButton = new Button("Cancel");
        cancelButton.addListener(Events.Select, new Listener<ButtonEvent>()
        {
            public void handleEvent(ButtonEvent e)
            {
                refresh();
            }
        });
        form.addButton(cancelButton);

        return form;
    }

    private void setIsNew(boolean isNew)
    {
        this.isNew = isNew;
    }

    private void setArtist(Artist artist)
    {
        this.artist = artist;
        bindToForm();
    }

    private void bindToModel()
    {
        Address address;
        if (artist.getAddress() == null)
        {
            address = new Address();
            artist.setAddress(address);
        } else
        {
            address = artist.getAddress();
        }
        address.setAddress1(address1Field.getValue());
        address.setAddress2(address2Field.getValue());
        address.setPostalCode(zipCodeField.getValue());
        address.setCity(cityField.getValue());
        if (countryCombo.getValue() != null)
        {
            address.setCountry(countryCombo.getValue().getCountry());
        }
        artist.setFacebookUser(facebookUserField.getValue());
        artist.setFlickrUser(flickrUserField.getValue());
        artist.setPhone(phoneField.getValue());
    }

    private void bindToForm()
    {
        if (artist.getAddress() != null)
        {
            address1Field.setValue(artist.getAddress().getAddress1());
            address2Field.setValue(artist.getAddress().getAddress2());
            zipCodeField.setValue(artist.getAddress().getPostalCode());
            cityField.setValue(artist.getAddress().getCity());
            if (artist.getAddress().getCountry() != null)
            {
                countryCombo.setValue(new CountryModel(
                        artist.getAddress().getCountry()));
            }
        }
        facebookUserField.setValue(artist.getFacebookUser());
        flickrUserField.setValue(artist.getFlickrUser());
        phoneField.setValue(artist.getPhone());
    }
}
